package otherapi.easyexcel;

import com.alibaba.excel.EasyExcel;
import org.junit.Test;

import java.io.*;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author LiDong
 * @version 1.0.0
 * @createTime 2/9/2023 5:23 PM
 */
public class ModeExcelTest {

    @Test
    public void noModelWrite() throws IOException {
        // 生成表头
        List<String> times = get96Times();
        List<List<String>> collect = times.stream().map(Arrays::asList).collect(Collectors.toList());
        List<InputStream> ins = new ArrayList<>();
        for (int i = 0; i < 2; i++) {
            // 使用 easyExcel 写到 OutputStream
            OutputStream out = new ByteArrayOutputStream();
            EasyExcel.write(out).head(collect).sheet("sheet1").doWrite(dataList());
            ins.add(outputStream2InputStream(out));
        }
        // 保存的 zip 文件名
        File zipFile = new File("./noModelWrite.zip");

        // 压缩包内流的文件名
        List<String> paths = Arrays.asList("1.xlsx", "2.xlsx");
        ZipUtil.zip(zipFile, paths, ins);
    }

    /**
     * 输出流转输入流；数据量过大请使用其他方法
     *
     * @param out
     * @return
     */
    private ByteArrayInputStream outputStream2InputStream(OutputStream out) {
        Objects.requireNonNull(out);
        ByteArrayOutputStream bos;
        bos = (ByteArrayOutputStream) out;
        return new ByteArrayInputStream(bos.toByteArray());
    }

    /**
     * 生成96个数据点
     *
     * @return
     */
    private List<String> get96Times() {
        List<String> res = new ArrayList<>(96);
        LocalDate localDate = LocalDate.now();
        LocalDateTime now = LocalDateTime.of(localDate.getYear(), localDate.getMonth(), localDate.getDayOfMonth(), 0, 0);
        for (int i = 1; i < 96; i++) {
            String format = now.plusMinutes(15 * i).format(DateTimeFormatter.ofPattern("HH:mm"));
            res.add(format);
        }
        res.add("24:00");
        return res;
    }

    /**
     * 生成表头对应的数据，这里有10行
     *
     * @return
     */
    private List<List<Object>> dataList() {
        List<List<Object>> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            List<Object> data = new ArrayList<>();
            for (int j = 0; j < 96; j++) {
                BigDecimal bigDecimal = new BigDecimal("0.5");
                double v = bigDecimal.add(BigDecimal.valueOf(new Random().nextDouble())).doubleValue();
                data.add(v);
            }
            list.add(data);
        }
        return list;
    }
}
